package com.antilia.infomaresme.data.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.antilia.infomaresme.data.dao.interfaces.ICategoriesDao;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.CategoryDimension;
/**
 * @author Ernesto
 *
 */
public class CategoriesDaoHibernateImp extends AbstractDaoHibernateImpl<Category> implements ICategoriesDao {

	public CategoriesDaoHibernateImp()
	{
		super(Category.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> findTopLevel() {
		Criteria criteria = getSession().get().createCriteria(Category.class);
		criteria.add(Restrictions.eq("topLevel", true));
		criteria.add(Restrictions.eq("dimension", CategoryDimension.business));
		criteria.addOrder(Order.asc("title"));		
		List<Category> list = (List<Category>)criteria.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> findTopLevelGeographic() {
		Criteria criteria = getSession().get().createCriteria(Category.class);
		criteria.add(Restrictions.eq("topLevel", Boolean.TRUE));
		criteria.add(Restrictions.eq("dimension", CategoryDimension.geographic));
		criteria.addOrder(Order.asc("title"));		
		List<Category> list = (List<Category>)criteria.list();
		return list;
	}
	
	public List<Category> findChildrem(Category category) {
		Criteria criteria = getSession().get().createCriteria(Category.class);
		criteria.add(Restrictions.eq("parent", category));
		@SuppressWarnings("unchecked")
		List<Category> list = (List<Category>)criteria.list();
		return list;
	}
}
